﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using WROA.Models.Entitys;
using WROA.Models.IServices;

namespace WROA.Models.Services
{
    public class DocumentService : Services, IDocumentService
    {
        /// <summary>
        /// 用户获取待审批的公文流程
        /// </summary>
        /// <param name="name"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public XPCollection GetAllApplyDocument(string name, string position)
        {
            GroupOperator goProcedure1 = new GroupOperator();
            goProcedure1.Operands.Add(new BinaryOperator("Pr_type", "人员", BinaryOperatorType.Equal));
            goProcedure1.Operands.Add(new BinaryOperator("Pr_typevalue", name, BinaryOperatorType.Equal));
            GroupOperator goProcedure2 = new GroupOperator();
            goProcedure2.Operands.Add(new BinaryOperator("Pr_type", "职位", BinaryOperatorType.Equal));
            goProcedure2.Operands.Add(new BinaryOperator("Pr_typevalue", position, BinaryOperatorType.Equal));
            GroupOperator goProcedure = new GroupOperator(GroupOperatorType.Or, goProcedure1, goProcedure2);

            XPCollection xpProcedure = new XPCollection(typeof(Procedure), goProcedure);
            XPCollection xpApplyDocument = null;
            foreach (Procedure p in xpProcedure)
            {
                GroupOperator go = new GroupOperator();
                go.Operands.Add(new BinaryOperator("Procedure", p, BinaryOperatorType.Equal));
                go.Operands.Add(new BinaryOperator("Ad_state", "未结束", BinaryOperatorType.Equal));
                XPCollection xp = new XPCollection(typeof(ApplyDocument), go);
                xpApplyDocument.AddRange(xp);
            }

            return xpApplyDocument;
        }

        /// <summary>
        /// 根据id返回一个公文审批对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApplyDocument GetApplyDocument(int id)
        {
            ApplyDocument applyDocument = null;
            XPCollection xp = new XPCollection(typeof(ApplyDocument), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyDocument ad in xp)
            {
                applyDocument = ad;
            }
            return applyDocument;
        }

        //public DocumentApprove GetDocumentApprove(ApplyDocument applyDocument)
        //{
        //    XPCollection xp = new XPCollection(typeof(DocumentApprove), new BinaryOperator("Applydocument", applyDocument, BinaryOperatorType.Equal));

        //}

        /// <summary>
        /// 根据流程和步骤获取步骤对象
        /// </summary>
        /// <param name="flow"></param>
        /// <param name="stepid"></param>
        /// <returns></returns>
        public Procedure GetProcedure(Flow flow, int stepid)
        {
            Procedure procedure = null;
            GroupOperator go = new GroupOperator();
            go.Operands.Add(new BinaryOperator("Flow", flow, BinaryOperatorType.Equal));
            go.Operands.Add(new BinaryOperator("Step_id", stepid, BinaryOperatorType.Equal));
            XPCollection xp = new XPCollection(typeof(Procedure), go);
            foreach (Procedure p in xp)
            {
                procedure = p;
            }
            return procedure;
        }

        /// <summary>
        /// 根据id更新流程步骤
        /// </summary>
        /// <param name="id"></param>
        /// <param name="procedure"></param>
        public void UpdateApplyDocumentProcedure(int id, Procedure procedure)
        {
            XPCollection xp = new XPCollection(typeof(ApplyDocument), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyDocument ad in xp)
            {
                ad.Procedure = procedure;
                ad.Save();
            }
        }

        /// <summary>
        /// 根据id更新公文申请流程为已经结束
        /// </summary>
        /// <param name="id"></param>
        public void UpdateApplyDocumentState(int id)
        {
            XPCollection xp = new XPCollection(typeof(ApplyDocument), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyDocument ad in xp)
            {
                ad.Ad_state = "已结束";
                ad.Save();
            }
        }

        /// <summary>
        /// 根据id更新公文申请流程已经批准
        /// </summary>
        /// <param name="id"></param>
        public void UpdateApplyDocumentIsAgree(int id)
        {
            XPCollection xp = new XPCollection(typeof(ApplyDocument), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyDocument ad in xp)
            {
                ad.Ad_isagree = "已批准";   //相反为不批准
                ad.Save();
            }
        }

        /// <summary>
        /// 保存公文审批信息，如果不批准流程，则修改公文申请表状态
        /// </summary>
        /// <param name="documentApprove"></param>
        public void SaveDocumentApprove(DocumentApprove documentApprove)
        {
            if (documentApprove.Da_result == "不批准")
            {
                documentApprove.Applydocument.Ad_state = "结束";
                documentApprove.Applydocument.Ad_isagree = "不批准";
            }
            documentApprove.Save();
        }
    }
}